/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.patientlist;

import com.example.patientlist.R;
import com.example.patientlist.R.id;
import com.example.patientlist.R.layout;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;



    /**
     * This is the "top-level" fragment, showing a list of items that the
     * user can pick.  Upon picking an item, it takes care of displaying the
     * data to the user as appropriate based on the currrent UI layout.
     */

    public class PrecordFragment extends ListFragment {
      //  boolean mDualPane;
       // int mCurCheckPosition = 0;
    	static final String[] records = new String[] {
    		"First Record",
    		"Second Record",
    		"Third Record",
    		"Fourth Record",
    		"Fifth Record",
    		"Sixth Record",
    		"Seventh Holmes",
    		"Eighth Knife",
    		"Ninth Beeblebrox"
    	};

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            // Populate list with our static array of titles.
            setListAdapter(this.new PrecordArrayAdapter(getActivity(),records));
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            

            // Check to see if we have a frame in which to embed the details
            // fragment directly in the containing UI.
          //  View detailsFrame = getActivity().findViewById(R.id.details);
          //  mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

           // if (savedInstanceState != null) {
                // Restore last state for checked position.
          //      mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
           // }

            //if (mDualPane) {
                // In dual-pane mode, the list view highlights the selected item.
             //   getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                // Make sure our UI is in the correct state.
          //      showDetails(mCurCheckPosition);
         //   }
        }
        
    	@Override
    	public void onListItemClick(ListView l, View v, int position, long id) {
     
    		//get selected items
    		String selectedValue = (String) getListAdapter().getItem(position);
    		Toast.makeText(getActivity(), selectedValue, Toast.LENGTH_SHORT).show();
     
    	}
     

//        @Override
//        public void onSaveInstanceState(Bundle outState) {
//            super.onSaveInstanceState(outState);
//            outState.putInt("curChoice", mCurCheckPosition);
//        }
//
//        @Override
//        public void onListItemClick(ListView l, View v, int position, long id) {
//            showDetails(position);
//        }
//
//        /**
//         * Helper function to show the details of a selected item, either by
//         * displaying a fragment in-place in the current UI, or starting a
//         * whole new activity in which it is displayed.
//         */
//        void showDetails(int index) {
//            mCurCheckPosition = index;
//
//            if (mDualPane) {
//                // We can display everything in-place with fragments, so update
//                // the list to highlight the selected item and show the data.
//                getListView().setItemChecked(index, true);
//
//                // Check what fragment is currently shown, replace if needed.
//                DetailsFragment details = (DetailsFragment)
//                        getFragmentManager().findFragmentById(R.id.details);
//                if (details == null || details.getShownIndex() != index) {
//                    // Make new fragment to show this selection.
//                    details = DetailsFragment.newInstance(index);
//
//                    // Execute a transaction, replacing any existing fragment
//                    // with this one inside the frame.
//                    FragmentTransaction ft = getFragmentManager().beginTransaction();
//                    if (index == 0) {
//                        ft.replace(R.id.details, details);
//                    } else {
//                        ft.replace(R.id.a_item, details);
//                    }
//                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//                    ft.commit();
//                }
//
//            } else {
//                // Otherwise we need to launch a new activity to display
//                // the dialog fragment with selected text.
//                Intent intent = new Intent();
//                intent.setClass(getActivity(), DetailsActivity.class);
//                intent.putExtra("index", index);
//                startActivity(intent);
//            }
//        }
    
    
        	public class PrecordArrayAdapter extends ArrayAdapter<String> {


    		private final Context context;
    		private final String[] values;
    		private final int [] image_resources = {R.drawable.windowsmobile_logo,R.drawable.ios_logo, R.drawable.blackberry_logo, R.drawable.android_logo  };
    		
    	 
    		public PrecordArrayAdapter(Context context, String[] values) {
    			super(context, R.layout.crazyrow_layout, values);
    			this.context = context;
    			this.values = values;
    		}
    	 
    		@Override
    		public View getView(int position, View convertView, ViewGroup parent) {
    			LayoutInflater inflater = (LayoutInflater) context
    				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	 
    			View rowView = inflater.inflate(R.layout.crazyrow_layout, parent, false);
    			TextView textView = (TextView) rowView.findViewById(R.id.label);
    			ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
    			textView.setText(values[position]);
    	 
    			// Change icon based on name
    			String s = values[position];
    	 
    			System.out.println(s); // is this for debugging?
    			
    			imageView.setImageResource(image_resources[position % 4]);
    	 
//    			if (s.equals("WindowsMobile")) {
//    				imageView.setImageResource(R.drawable.windowsmobile_logo);
//    			} else if (s.equals("iOS")) {
//    				imageView.setImageResource(R.drawable.ios_logo);
//    			} else if (s.equals("Blackberry")) {
//    				imageView.setImageResource(R.drawable.blackberry_logo);
//    			} else {
//    				imageView.setImageResource(R.drawable.android_logo);
//    			}
    	 
    			return rowView;
    		}
    	}
    	
    }

